﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CVS_log_parser
{
    public class CvsFileInfo : CvsFileInfoBase
    {
        internal CvsFileInfo( string rawString )
        {
            var parts = rawString.Split( new string[] { "----------------------------" + Environment.NewLine, "----------------------------" }, StringSplitOptions.RemoveEmptyEntries ).Select( x => x.EndsWith( Environment.NewLine ) ? x.Remove( x.Length - 2 ) : x ).ToArray();
            var data = parts[0].Replace( "\t", "" ).Split( new string[] { Environment.NewLine, ";" }, StringSplitOptions.RemoveEmptyEntries );
            var dict = new Dictionary<string, string>();

            data.ToList().ForEach( x => dict.Add( x.Split( ':' )[0], x.Split( ':' )[1] ) );

            this.RCSFileName = dict["RCS file"].Trim();
            this.WorkingFileName = dict["Working file"].Trim();
            this.Branch = dict["branch"].Trim();
            this.Locks = dict["locks"].Trim();
            this.AccessList = dict["access list"].Trim();
            this.SymbolicNames = dict["symbolic names"].Trim();
            this.KeywordSubstitution = dict["keyword substitution"].Trim();
            this.TotalRevisions = int.Parse( dict["total revisions"] );
            this.SelectedRevisions = int.Parse( dict["selected revisions"] );
            this.Description = dict["description"].Trim();
            this.Revisions = new List<CvsFileRevision>();

            parts.Skip( 1 ).ToList().ForEach( x => this.Revisions.Add( new CvsFileRevision( x, this ) ) );

            this.HeadRevision = this.Revisions.First( x => string.Compare( x.Revision, dict["head"].Trim(), true ) == 0 );
        }
    }
}
